var app = getApp()
Page({
    data: {
        code: 1
    },

    onLoad() {
        var that = this
        const userinfo = tt.getStorageSync('userinfo')
        if (!userinfo) {
            app.wxlogin().then(function (res) {
                that.setData({
                    userinfo: res
                }, () => {
                    // that.getdata()
                })
            })
        } else {
            that.setData({
                userinfo
            }, () => {
                that.getdata()
            })
        }
    },

    getdata() {
        var that = this
        const { uid, openid } = that.data.userinfo
        const data = { uid, openid }
        console.log(data)
        app.api_request('?c=signup_pay_tt', 'GET', data).then(function (res) {
            if (res) {
                that.setData({
                    code: 1,
                    pagedata: res
                })
                tt.setNavigationBarTitle({
                    title: res.appTitle
                })
            }
        })
    },
    onPay(event) {
        const { status, orderId, outOrderNo, result } = event.detail;
        if (status === 'success') {
            const { code } = result;
            if (code === 0) {
                // 支付成功
            } else {
                // 支付失败（超时、取消、关闭）
            }
        } else {
            const { errMsg } = result;
        }
    },
    userLogin(event) {
        const { goodsId, goodsType } = event.detail
        return new Promise((resolve, reject) => {
            tt.login({
                success() {
                    // 用户登录成功并获取信息，则调用 resolve 函数，跳转至提单页
                    resolve();
                },
                fail() {
                    // 用户登录失败，则跳转提单页失败
                    reject();
                }
            });
        });
    },
    getGoodsInfo(event) {
        return new Promise(resolve => {
            resolve({
                minLimits: 1,
                maxLimits: 1,
                dateRule: '周一至周日可用',
                relationType: 1,
                extra: {
                    use_trade_delivery: 1
                },
                validation: {
                    phoneNumber: {
                        required: false  // 手机号是否必填 
                    },
                    reservationType: 1,
                    reservationCount: 2,
                },
                // 在 bind:getgoodsinfo 返回的 promise 的 resolve 函数中新增 marketingVersion 字段 
                marketingVersion: 2,
            });

        });
    },
    // js
    // bind:consume 使用示例
    orderChangeHandler(event) {
        const { orderId } = event.detail
        console.log(orderId)
        //do something
    },

    payment() {
        var that = this
        // const { paydata } = that.data.pagedata

        console.log(tt.canIUse('createOrder'))


        // tt.pay({
        //     orderInfo: paydata.data,
        //     service: 5,
        //     success(res) {
        //         if (res.code == 0) {
        //             tt.showModal({
        //                 content: '缴费成功！请携带证件前往驾校完成体检入档',
        //                 showCancel: false,
        //                 success: function () {
        //                     tt.reLaunch({
        //                         url: "/pages/user/index"
        //                     })
        //                 }
        //             })
        //         } else {
        //             tt.switchTab({
        //                 url: "/pages/index/index"
        //             })
        //         }
        //     },
        //     fail(res) {
        //         console.log(res)
        //     }
        // })
    },


    refund() {
        tt.applyRefund({
            refundInfo: {
                reason: ['测试退款'], // 退款理由 必填
                note: '不需要', // 备注 非必填
            },
            outOrderNo: 'LD1719814639668249ef8ac45', // 外部订单号 必填
            goodsList: [
                {
                    goodsId: '1803343778623547', // 商品ID 必填
                    goodsType: 2, // 商品类型 必填
                    quantity: 1, // 退款商品数量 必填
                },
            ], // 退款商品列表 新订单全部使用该数组退款 refundGoodsList和refundTotalAmount必填一个

            callbackData: { test: 123 }, // 透传数据 会被 JSON.stringfy 化 非必填
            // refundTotalAmount:1, // 老订单指定金额退款，新订单该参数无效
            success: res => {
                const { orderId, outRefundNo, refundId } = res;
                console.log('申请退款成功');
                console.log('orderId', orderId, 'outRefundNo', outRefundNo, 'refundId', refundId);
            },
            fail: res => {
                const { errNo, errMsg, errLogId } = res;
                console.log('申请失败', errNo, errMsg, errLogId);
            },
        });

    },
    handleError(event) {
        const { errMsg, errNo } = event.detail;
        console.log(event.detail)
    },
    // bind:refund 使用示例 
    handleRefund(event) {
        const { status, result } = event.detail;
        console.log(event.event)
        if (status === 'success') {
            const { refundId, outRefundNo } = result;
            console.log(refundId)
        } else {
            const { errMsg } = result;
            console.log(errMsg)
        }
    },

    // bind:applyrefund 使用示例 
    applyRefund(event) {
        const { orderId } = event.detail;
        const extra = { orderId }; // 开发者需要透传的参数，可自定义内容 

        return new Promise(resolve => {
            resolve(extra);
        });
    },





})